<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :node-data="companyInfo" :is-root="true" @add="addDepartment" />
        <hr>
        <el-tree
          :data="depts"
          default-expand-all
          :expand-on-click-node="false"
        >
          <template #default="obj">
            <!-- 用了一个行列布局 -->
            <TreeTools
              :node-data="obj.data"
              @del="getdepartments"
              @add="addDepartment"
              @edit="editDepartment"
            />
          </template>
        </el-tree>
      </el-card>
      <AddDept
        ref="addDept"
        :is-show-dialog.sync="isShowDialog"
        :node-data="nodeData"
        @add="getdepartments"
      />
    </div>
  </div>
</template>

<script>
import AddDept from './components/add-dept.vue'
import TreeTools from './components/tree-tools.vue'
import { getdepartments } from '../../api/departments'
import { listToTreDate } from '../../utils/index'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      companyInfo: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      depts: [],
      isShowDialog: false,
      nodeData: {}
    }
  },
  async created() {
    this.getdepartments()
  },
  methods: {
    async getdepartments() {
      const { depts } = await getdepartments()
      console.log(depts)
      this.depts = listToTreDate(depts, '')
    },
    addDepartment(nodeData) {
      // console.log('60', nodeData)
      this.isShowDialog = true
      this.nodeData = nodeData
    },
    async editDepartment(nodeData) {
      await this.$refs.addDept.getDepartmentDetail(nodeData.id)
      this.isShowDialog = true
      this.nodeData = nodeData
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
